package bma.common.netty.thrift.dual.sample;

import java.util.Map;

import org.apache.thrift.TBase;
import org.apache.thrift.TException;

import bma.common.langutil.ai.stack.AIStack;
import bma.common.netty.thrift.dual.NTSBaseServiceClient;
import bma.common.netty.thrift.dual.NTSClient;
import bma.common.netty.thrift.dual.impl.NTSBaseProcessor;
import bma.common.netty.thrift.dual.impl.NTSProcessFunction;
import bma.common.thrift.ai.AIBaseStack;
import bma.common.thrift.sample.Hello.error_args;
import bma.common.thrift.sample.Hello.error_result;
import bma.common.thrift.sample.Hello.name_args;
import bma.common.thrift.sample.Hello.name_result;
import bma.common.thrift.sample.Hello.say_args;
import bma.common.thrift.sample.Hello.say_result;
import bma.common.thrift.sample.Hello.sleep_args;
import bma.common.thrift.sample.Hello.sleep_result;
import bma.common.thrift.sample.Hello4AI;
import bma.common.thrift.sample.Hello4AI.Iface;

public class Hello4NTS {

	public static class Processor<IFACE extends Hello4AI.Iface> extends
			NTSBaseProcessor<IFACE> {

		public Processor(IFACE iface) {
			super(iface);
		}

		@Override
		protected Map createProcessFunctionMap() {
			Map map = super.createProcessFunctionMap();
			map.put("say", new say());
			map.put("name", new name());
			map.put("error", new error());
			map.put("sleep", new sleep());
			return map;
		}

		private static class say<I extends Iface> extends
				NTSProcessFunction<I, say_args> {
			public say() {
				super("say");
			}

			@Override
			protected say_args getEmptyArgsInstance() {
				return new say_args();
			}

			protected boolean getResult(AIStack<TBase> stack, I iface,
					say_args args) throws org.apache.thrift.TException {
				say_result result = new say_result();
				return iface.say(new AIBaseStack<Boolean>(stack, result),
						args.getWord());
			};

		}

		private static class name<I extends Iface> extends
				NTSProcessFunction<I, name_args> {
			public name() {
				super("name");
			}

			@Override
			protected name_args getEmptyArgsInstance() {
				return new name_args();
			}

			@Override
			protected boolean getResult(AIStack<TBase> stack, I iface,
					name_args args) throws org.apache.thrift.TException {
				name_result result = new name_result();
				return iface.name(new AIBaseStack<String>(stack, result),
						args.getTitle());
			}
		}

		private static class error<I extends Iface> extends
				NTSProcessFunction<I, error_args> {
			public error() {
				super("error");
			}

			@Override
			protected error_args getEmptyArgsInstance() {
				return new error_args();
			}

			@Override
			protected boolean getResult(AIStack<TBase> stack, I iface,
					error_args args) throws org.apache.thrift.TException {
				error_result result = new error_result();
				return iface.error(new AIBaseStack<Boolean>(stack, result),
						args.getMsg());
			}
		}

		private static class sleep<I extends Iface> extends
				NTSProcessFunction<I, sleep_args> {
			public sleep() {
				super("sleep");
			}

			@Override
			protected sleep_args getEmptyArgsInstance() {
				return new sleep_args();
			}

			@Override
			protected boolean getResult(AIStack<TBase> stack, I iface,
					sleep_args args) throws org.apache.thrift.TException {
				error_result result = new error_result();
				return iface.sleep(new AIBaseStack<Boolean>(stack, result),
						args.getTime());
			}
		}
	}

	public static class Client extends NTSBaseServiceClient implements Iface {

		public Client(NTSClient client, String module) {
			super(client, module);
		}

		@Override
		public boolean say(AIStack<Boolean> stack, String word)
				throws TException {
			return super.invoke(stack, new say_result(), "say", new say_args(
					word));
		}

		@Override
		public boolean name(AIStack<String> stack, String title)
				throws TException {
			return super.invoke(stack, new name_result(), "name",
					new name_args(title));
		}

		@Override
		public boolean error(AIStack<Boolean> stack, String msg)
				throws TException {
			return super.invoke(stack, new error_result(), "error",
					new error_args(msg));
		}

		@Override
		public boolean sleep(AIStack<Boolean> stack, int time)
				throws TException {
			return super.invoke(stack, new sleep_result(), "sleep",
					new sleep_args(time));
		}
	}

}
